;(function () {

    var toast = function (mes) {
        var toastDOM = '<div class="hl-toast">' + mes + '</div>'
        $('body').append(toastDOM)
        setTimeout(
          function () {
            $('.hl-toast').remove()
          },
          2000
        )
    }

    var hl = {}
    window.hl = hl
    window.hl.toast = toast

}())

var hllayer = (function () {

    var conf = {
        id: 0,
        zindex: 999,
        init: false
    };

    var haolou = {
        init:function () {
            var id = conf.id = conf.id + 1;
            this.mask();
            return id;
        },
        center: function (taget) {
            taget.css({
                top: '50%',
                left: '50%',
                'marginTop': taget.height() * -0.5,
                'marginLeft': taget.width() * -0.5
            });
        },
        mask: function () {
            conf.zindex = conf.zindex + 2;
            $('body').prepend('<div id="mask-' + conf.id + '" class="hllayer-mask" style="z-index:' + conf.zindex + ';"></div>');
        },
        close: function (i) {
            if (i == undefined) this.closeall();
            else {
                $('#hllayer-' + i + ',' + '#mask-' + i).fadeOut(350, function () {
                    $('#elayer-' + i).remove();
                    $('#mask-' + i).remove();
                });
            }
        },
        closeall: function () {
            $('.hllayer-mask').remove();
            $('.hllayer-view').remove();
        },
        toast:function (mes) {
            var self = this;
            conf.zindex = conf.zindex + 1
            $('body').prepend('<div id="mask-' + conf.id + '" class="hllayer-toast" style="z-index:' + conf.zindex + ';">' + mes + '</div>')

            setTimeout(function () {
                self.close(conf.id)
            },2000)
        },
        tips:function (container,mes,flag) {
            if(typeof mes === 'number') mes = mes.toString();
            var mesArr = mes.split(','),
                ctx = '';

            mesArr.map(function (_item) { ctx += '<div class="hllayer-tip-mes">' + _item + '</div>' })
            var tips = $('<div id="hllayer-' + conf.id + '" class="hllayer-tips hllayer-view" style="left:' + left + 'px; top:' + top + 'px;display: block;z-index:"' + conf.zindex
                + '>' + ctx + '</div>')

            $('body').append(tips)

            var l = container.offset().left,
                t = container.offset().top,
                w = container.outerWidth(),
                h = container.outerHeight(),
                mesw = $('.hllayer-tips').outerWidth(),
                left = l + (w-mesw)/2,
                top = t + h ;

            if(!flag){ top = t + h;}
            if(flag){ top = t + h + 17;}
            $('.hllayer-tips').css({
                top: top,
                left: left,
                display:'block'
            })
        },
        confirm:function (c,yescall,nocall,closecall) {
            var self = this
            this.init()
            conf.init = true
            var cfm = $('<div class="hllayer-confirm">'
                + '<div class="mes">' + c + '</div>'
                + '<div class="btn-wrapper">'
                + '<a class="yes">确认删除</a>'
                + '<a class="no">取消</a></div></div>')

            cfm.find('.no').click(function () {
                if(nocall)nocall()
                else self.close(conf.id);
            })
            cfm.find('.yes').click(function () {
                if(yescall)yescall()
                $('.hllayer-mask').remove();
                $('.hllayer-view').remove();
            })
            return this.page(cfm, closecall);
        },
        confirm1:function (c,yescall,nocall,closecall) {
            var self = this
            this.init()
            conf.init = true
            var cfm = $('<div class="hllayer-confirm">'
                + '<div class="mes">' + c + '</div>'
                + '<div class="btn-wrapper">'
                + '<a class="yes">确认</a>'
                + '<a class="no">取消</a></div></div>')

            cfm.find('.no').click(function () {
                if(nocall)nocall()
                self.close(conf.id);
            })
            cfm.find('.yes').click(function () {
                if(yescall)yescall()
                $('.hllayer-mask').remove();
                $('.hllayer-view').remove();
            })
            return this.page(cfm, closecall);
        },
        confirm2:function (c,yescall,nocall,closecall) {
            var self = this
            this.init()
            conf.init = true
            var cfm = $('<div class="hllayer-confirm">'
                + '<div class="mes">' + c + '</div>'
                + '<div class="btn-wrapper">'
                + '<a class="yes">保存</a>'
                + '<a class="no">取消</a></div></div>')

            cfm.find('.no').click(function () {
                if(nocall)nocall()
                else self.close(conf.id);
            })
            cfm.find('.yes').click(function () {
                if(yescall)yescall()
                $('.hllayer-mask').remove();
                $('.hllayer-view').remove();
            })
            return this.page(cfm, closecall);
        },
        confirm3:function (c,yescall,nocall,closecall) {
            var self = this
            this.init()
            conf.init = true
            var cfm = $('<div class="hllayer-confirm">'
                + '<div class="mes">' + c + '</div>'
                + '<div class="btn-wrapper oneBut">'
                + '<a class="yes">确认</a>'
                + '</div></div>')

            cfm.find('.no').click(function () {
                if(nocall)nocall()
                else self.close(conf.id);
            })
            cfm.find('.yes').click(function () {
                if(yescall)yescall()
                $('.hllayer-mask').remove();
                $('.hllayer-view').remove();
            })
            return this.page(cfm, closecall);
        },
        prompt:function (options) {
          var self = this;
          var defaultObj = {
            title:'',
            placeholder:'',
            leftBtn:'确认',
            rightBtn:'取消',
            subTitle: '',
            tips: ''
          }
          var options = $.extend({},defaultObj,options)
          this.init()
          conf.init = true;
          conf.zindex = conf.zindex + 1
          var subtitleFlag = 'hide', tipsFlag = 'hide';
          if (options.subTitle) {
            subtitleFlag = ''
          }
          if (options.tips) {
            tipsFlag = ''
          }
          var pmt = $('<div class="hllayer-prompt hllayer-view pop box-o-shadow" id="hllayer-' + conf.id + '" + data-id="' + conf.id + '" style="z-index:' + conf.zindex +'">'
            + '<div class="head-pop font-16"><span>' + options.title + '</span>'
            + '<button class="fw-close absolute close" type="button">×</button>'
            + '</div>'
            + '<div class="content-pop ptb-10 plr-24">'
            + '<textarea name="name" rows="8" class="form-control mes" placeholder="' + options.placeholder + '"></textarea>'
            + '<p class="font-14 color-999 mt-5 '+subtitleFlag+'">'+options.subTitle+'</p>'
            + '<p class="font-14 '+tipsFlag+'" style="cursor: Auto; color: #337ab7;">'+options.tips+'</p>'
            + '</div>'
            + '<div class="ptb-10 text-center">'
            + '<button type="button" class="n-btn-primary mr-10 yes">' + options.leftBtn + '</button>'
            + '<button type="button" class="n-btn-outline no">' + options.rightBtn + '</button>'
            + '</div>'
          + '</div>')

          pmt.find('.no').click(function () {
            if(options.cancel)options.cancel()
            else self.close(conf.id);
          })
          pmt.find('.yes').click(function () {
            var mes = $('.hllayer-prompt .mes').val().trim()
            if(options.confirm)options.confirm(mes)
            else self.close(conf.id);
          })
          pmt.find('.close').click(function () {
            self.close(conf.id)
            if(options.close)options.close()
          })

          $('body').prepend(pmt)

        },
        promptSm:function (options) {
          var self = this;
          var defaultObj = {
            title:'',
            placeholder:'',
            leftBtn:'确认',
            rightBtn:'取消',
            subTitle: '',
            tips: ''
          }
          var options = $.extend({},defaultObj,options)
          this.init()
          conf.init = true;
          conf.zindex = conf.zindex + 1
          var subtitleFlag = 'hide', tipsFlag = 'hide';
          if (options.subTitle) {
            subtitleFlag = ''
          }
          if (options.tips) {
            tipsFlag = ''
          }
          var pmt = $('<div class="hllayer-prompt hllayer-view pop box-o-shadow" id="hllayer-' + conf.id + '" + data-id="' + conf.id + '" style="z-index:' + conf.zindex +'">'
            + '<div class="head-pop font-16"><span>' + options.title + '</span>'
            + '<button class="fw-close absolute close" type="button">×</button>'
            + '</div>'
            + '<div class="content-pop ptb-10 plr-24">'
            + '<div class="pull-left">说明：</div>'
            + '<div class="pull-left" style="width:94%">'
            + '<textarea name="name" rows="8" class="form-control mes msgsssss" placeholder="' + options.placeholder + '"></textarea>'
            + '<p class="font-14 color-999 mt-5 '+subtitleFlag+'">'+options.subTitle+'</p>'
            + '<p class="font-14 '+tipsFlag+'" style="cursor: Auto; color: #337ab7;">'+options.tips+'</p>'
            + '</div>'
            + '</div>'
            + '<div class="ptb-10 text-center">'
            + '<button type="button" class="n-btn-primary mr-10 yes">' + options.leftBtn + '</button>'
            + '<button type="button" class="n-btn-outline no">' + options.rightBtn + '</button>'
            + '</div>'
          + '</div>')

          pmt.find('.no').click(function () {
            if(options.cancel)options.cancel()
            else self.close(conf.id);
          })
          pmt.find('.yes').click(function () {
            var mes = $('.hllayer-prompt .msgsssss').val().trim()
            if(options.confirm)options.confirm(mes)
            else self.close(conf.id);
          })
          pmt.find('.close').click(function () {
            self.close(conf.id)
            if(options.close)options.close()
          })

          $('body').prepend(pmt)

        },
        loading:function () {
            this.init()
            var load = $('' +
                '<div id="hllayer-' + conf.id + '" class="hllayer-view" style="z-index:' + (conf.zindex + 1) + ';">' +
                '<div class="hllayer-loading"></div>' +
                '</div>');

            $('body').prepend(load);
            return conf.id;
        },
        page:function (c,closecall) {
            var self = this
            if(!conf.init){
                this.init()
            }
            conf.zindex = conf.zindex + 1
            var page = $('<div class="hllayer-page hllayer-view" id="hllayer-' + conf.id + '" data-id="' + conf.id + '" style="z-index:' + conf.zindex +'">'
                + '<div class="title"><button class="close">×</button></div>'
                + '<div class="content"></div></div>')

            page.find('.content').append(c)
            page.find('.close').click(function () {
                self.close(page.data('id'))
                if(closecall)closecall()
            })
            $('body').append(page)
            conf.init = false
            return conf.id
        }
    }

    return {
        // 友好提醒框  mes 提示内容
        toast:function (mes) {
            return haolou.toast(mes)
        },
        // 确认框 c:内容 yescall:确认回调 nocall:取消回调 closecall:关闭回调
        confirm:function (c,yescall,nocall,closecall) {
            return haolou.confirm(c,yescall,nocall,closecall)
        },
        confirm1:function (c,yescall,nocall,closecall) {
            return haolou.confirm1(c,yescall,nocall,closecall)
        },
        confirm2:function (c,yescall,nocall,closecall) {
            return haolou.confirm2(c,yescall,nocall,closecall)
        },
        confirm3:function (c,yescall,nocall,closecall) {
            return haolou.confirm3(c,yescall,nocall,closecall)
        },
        // 带留言信息的
        // options: title 标题    placeholder 输入框提示信息  leftBtn 左侧按钮内容 rightBtn 右侧按钮内容
        // confirm  确认回调 参数为输入框内信息   cancel:取消回调   close:关闭时回调
        prompt:function (options) {
            return haolou.prompt(options)
        },
        promptSm: function (options) {
            return haolou.promptSm(options)
        },
        // 详情展示框 c:容器 mes:内容 flag:详情定位靠下方
        tips:function (container,mes,flag) {
            return haolou.tips(container,mes,flag)
        },
        // 加载中的动画
        loading:function () {
            return haolou.loading()
        },
        //页面内容框(HTML)
        page: function (t, c, closecall) {
            return haolou.page(t, c, closecall);
        },
        // 关闭
        close:function (i) {
            haolou.close(i)
        },
        //关闭全部
        closeall: function () {
            haolou.closeall();
        },
    }
}())

// hllayer.confirm('发动机卡手机看空间',
//     function () {
//         alert('成功回调')
//     },
//     function () {
//         alert('取消回调')
//     },
//     function () {
//         alert('关闭回调')
//     }
// )

// hllayer.toast(message)
// hllayer.loading()
// hllayer.tips($('.show-container'),message)

// hllayer.page()

// var prompt = {
//   title:'审核拒绝',   // 标题  默认为空
//   placeholder:'审核不通过原因说明（非必填）',  // 输入框提示信息 默认为空
//   leftBtn:'提交',   // 左侧按钮内容  默认为确认
//   rightBtn:'取消',  // 右侧按钮内容  默认为取消
//   confirm:function (mes) {
//     alert('恭喜你,你已成功提交！！！' + mes)
//   },
//   cancel:function () {
//     alert('呜呜,你取消了哦！！！')
//   },
//   close:function () {
//     alert('关闭了窗口哦！！！')
//   }
// }
// hllayer.prompt(prompt)
